//
//  HDRewritePageControl.swift
//  HuaDaoPeifu
//
//  Created by liangyuanliangyuan on 16/5/18.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit

class HDRewritePageControl: UIPageControl {

    
    var _pageImageWidth:CGFloat = 5
    var _pageImageHeight:CGFloat = 5
    
    var pageImage:String? {
        didSet{
            let tempImg = UIImage.init(named: self.pageImage!)
            _pageImageWidth = (tempImg?.size.width)!
            _pageImageHeight = (tempImg?.size.height)!
        }
    }
    var currentPageImage:String?
    
    override var currentPage:Int
        {
        didSet {
            
            updateDots()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.currentPageIndicatorTintColor = UIColor.white
        self.pageIndicatorTintColor = UIColor.gray
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //更新界面显示
    func updateDots () {
        if self.subviews.count == 0 {
            return
        }
        //self.subviews是点的数组，取出来自定义page点显示
        for i:Int in 0...self.subviews.count - 1 {
            let dot:UIView = self.subviews[i]
            dot.backgroundColor = UIColor.clear
            let size:CGSize = CGSize(width: _pageImageWidth, height: _pageImageHeight)
            
            dot.frame = CGRect(x: dot.frame.origin.x, y: dot.frame.origin.y, width: size.width, height: size.width)
            var dotImg:UIImageView? = dot.viewWithTag(101) as? UIImageView
            if dotImg == nil {
                dotImg = UIImageView.init(frame: CGRect(x: 0, y: 0, width: _pageImageWidth, height: _pageImageHeight))
                dotImg!.tag = 101
                dot.addSubview(dotImg!)
            }
            
            if i == self.currentPage {
                dotImg!.image =  UIImage.init(named: currentPageImage!)
            } else {
                dotImg!.image =  UIImage.init(named: pageImage!)
            }
        }
    }

    
    
    
    
    
    
//    var currentImage: UIImage?
//    var InactiveImage: UIImage?
//    
//    init(frame: CGRect,currentImageName: String,indicatorImageName: String) {
//        super.init(frame: frame)
//        currentImage = UIImage(named: currentImageName)
//        InactiveImage = UIImage(named: indicatorImageName)
//        
//        
//    }
//
//    func uploadDots() {
//        for i in 0..<self.subviews.count {
//            let dot: UIImageView = self.subviews[i] as! UIImageView
//            
//            if i == currentPage {
//                dot.image = currentImage
//            }else {
//                dot.image = InactiveImage
//            }
//        }
//    }
//    
//   override var currentPage: Int {
//        set {
//            super.currentPage = currentPage
//        }
//        get {
//            uploadDots()
//            return super.currentPage
//        }
//    }
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }

}
